
package com.cxx.purchasecharge.core.util;

import java.security.Key;

/**
 * This class provides some static conversion methods.
 */
public class HexConvert
{
    // all methods are static, this should never be instantiated
    private HexConvert()
    {
    }

    /**
     * Converts a Key into a hex String for transport. The hex string will have two characters per
     * byte
     * 
     * @param key
     *            A public or private key to convert
     * @return a Hex string representation of the key
     */
    public static String convertHexString(Key key)
    {
        if (key == null)
        {
            return (null);
        }

        return (convertHexString(key.getEncoded()));
    }

    /**
     * Convert byte array to hex string. The hex string will have two characters per byte.
     * 
     * @param array
     *            The array of bytes to convert
     * @return The hex string representation
     */
    public static String convertHexString(byte[] array)
    {
        return (convertHexString(array, 0, array.length));
    }

    /**
     * Convert byte array to hex string. The hex string will have two characters per byte.
     * 
     * @param array
     *            The array of bytes to convert
     * @param offset
     *            The offset in array to begin conversion
     * @param count
     *            The number of bytes to convert
     * @return The hex string representation
     */
    public static String convertHexString(byte[] array, int offset, int count)
    {

        StringBuffer hexString = new StringBuffer();

        for (int i = offset; i < count; i++)
        {
            if (i == array.length)
                break;

            byte c = array[i];
            int j = (c < 0) ? 256 + c : c;

            String digitStr = Integer.toHexString(j);
            if (digitStr.length() == 1)
            {
                hexString.append("0");
            }

            hexString.append(digitStr);
        }

        return hexString.toString();
    }

    /**
     * Convert hex string to byte array The hex string is assumed to have exactly two characters per
     * byte.
     * 
     * @param hexString
     *            The hex string to convert
     * @return The byte array
     */
    public static byte[] convertToBytes(String hexString)
    {

        byte bytes[] = new byte[hexString.length() / 2];

        for (int i = 0; i < hexString.length(); i += 2)
        {
            bytes[i / 2] = (byte) Integer.parseInt(hexString.substring(i, i + 2), 16);
        }

        return bytes;
    }

    /**
     * convertChar Converts a hexString to a character String.
     * 
     * @param hexString
     *            the string in Hex
     * @return a String of characters
     */
    public static String convertChar(String hexString)
    {
        String retVal = new String();

        for (int i = 0; i < hexString.length(); i += 2)
        {
            retVal = retVal + Integer.valueOf(hexString.substring(i, i + 2), 16).toString();
        }
        return retVal;
    }

}
